Namespace Controls
    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
    Public Class DataGridColumn
        Inherits Windows.Forms.DataGridViewColumn

        Sub New()
            MyBase.new(New Controls.DataGridCell)
        End Sub

        Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridViewCell)
                If Not value.GetType.IsAssignableFrom(GetType(Controls.DataGridCell)) Then
                    Throw New ArgumentException("value not derived from DataGridViewColourPickerCell.")
                Else
                    MyBase.CellTemplate = value
                End If
            End Set
        End Property

        Public Overrides Function GetPreferredWidth(ByVal autoSizeColumnMode As System.Windows.Forms.DataGridViewAutoSizeColumnMode, ByVal fixedHeight As Boolean) As Integer
            Select Case autoSizeColumnMode
                Case DataGridViewAutoSizeColumnMode.AllCells, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
                    Dim nWidth As Integer = 20

                    If autoSizeColumnMode = DataGridViewAutoSizeColumnMode.AllCells Then
                        nWidth = Controls.DataGridCell.MeasureTextSize(Me.DataGridView.CreateGraphics, Me.HeaderText, Me.CellTemplate.Style.Font, TextFormatFlags.VerticalCenter).Width + 5
                    End If

                    If Me.DataGridView.RowCount > 0 Then
                        For i As Integer = 0 To Me.DataGridView.RowCount - 1
                            nWidth = Math.Max(CType(Me.DataGridView(Me.Index, i), Controls.DataGridCell).PreferredSize.Width, nWidth)
                        Next
                    End If

                    Return nWidth
                Case Else
                    Return MyBase.GetPreferredWidth(autoSizeColumnMode, fixedHeight)
            End Select
        End Function

    End Class
End Namespace